package com.zhuizikejithreeig.cn.threeigkeji.loginfo;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.zhuizikejithreeig.cn.been.ForgNumberBeen;
import com.zhuizikejithreeig.cn.contect.ContectUtils;
import com.zhuizikejithreeig.cn.contect.UriContect;
import com.zhuizikejithreeig.cn.threeigkeji.BasicActivity;
import com.zhuizikejithreeig.cn.threeigkeji.CompontApplayCation;
import com.zhuizikejithreeig.cn.threeigkeji.R;
import com.zhuizikejithreeig.cn.utils.GsonUtil;
import com.zhuizikejithreeig.cn.utils.ToastMake;
import com.zhuizikejithreeig.cn.utils.Utils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.MediaType;

public class ChengPawTwoActivity extends BasicActivity {

    private Button   mSubmit;
    private EditText mNewPwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView_V(R.layout.activity_cheng_paw_two);
        setLeftShow(1, R.mipmap.back);

        getLift_V().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        setTitle_V("更换密码");

        mNewPwd = (EditText) findViewById(R.id.name3);
        mSubmit = (Button) findViewById(R.id.name8);

        mSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isConnected()) {

                    //                    submit();

                    newSubmit();
                }
            }
        });
    }

    private void newSubmit() {
        String trim = mNewPwd.getText().toString().trim();
        if (TextUtils.isEmpty(trim)) {
            ToastMake.showToast("密码不能为空");
            return;
        }
        if (trim.length() < 6 || trim.length() > 25) {
            ToastMake.showToast("请输入6-25位长度的密码");
            return;
        }
        showProgrees("修改中,请稍等.....");
        OkHttpUtils
                .post()
                .url(UriContect.PASSWORD)
                .addParams("phone", Utils.getString(ContectUtils.PHONESAVE))
                .addParams("pwd", trim)
                .addParams("code", CompontApplayCation.registCode)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        ToastMake.showToast(getResources().getString(R.string.string58));
                        hideProgree();
                    }
                    @Override
                    public void onResponse(String response, int id) {
                        hideProgree();
                        ForgNumberBeen bean = GsonUtil.parseJsonToBean(response, ForgNumberBeen.class);
                        if (bean == null) {
                            ToastMake.showToast(getResources().getString(R.string.string58));
                            return;
                        }
                        if (!TextUtils.equals(bean.getStatus(), "200")) {
                            ToastMake.showToast(bean.getMsg());
                            return;
                        } else {
                            ToastMake.showToast(bean.getMsg());
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    skipOtherActivity(SettingActivity.class, true);
                                    finish();
                                }
                            });
                        }
                    }
                });
    }

    //    private void submit() {
    //
    //        Map<String, String> map = new HashMap<>();
    //        map.put("phone", Utils.getString(ContectUtils.PHONESAVE));
    //        map.put("pwd", trim);
    //        map.put("code", CompontApplayCation.registCode);
    //        final String json = GsonUtil.parseMapToJson(map);
    //
    //        new Thread(new Runnable() {
    //            @Override
    //            public void run() {
    //
    //                OkHttpUtils
    //                        .postString()
    //                        .url(UriContect.PASSWORD)
    //                        .content(json)
    //                        .mediaType(MediaType.parse("application/json; charset=utf-8"))
    //                        .build()
    //                        .execute(new StringCallback() {
    //                            @Override
    //                            public void onError(Call call, Exception e, int id) {
    //                                ToastMake.showToast(getResources().getString(R.string.string58));
    //                                hideProgree();
    //                            }
    //
    //                            @Override
    //                            public void onResponse(String response, int id) {
    //                                hideProgree();
    //                                    ForgNumberBeen bean = GsonUtil.parseJsonToBean(response, ForgNumberBeen.class);
    //                                if (bean == null) {
    //                                    ToastMake.showToast(getResources().getString(R.string.string58));
    //
    //                                    return;
    //                                }
    //                                if (!TextUtils.equals(bean.getStatus(),"1")){
    //                                    ToastMake.showToast(bean.getMsg());
    //                                    return;
    //                                }else {
    //                                    ToastMake.showToast(bean.getMsg());
    //                                    runOnUiThread(new Runnable() {
    //                                        @Override
    //                                        public void run() {
    //                                            skipOtherActivity(SettingActivity.class,true);
    //                                            finish();
    //                                        }
    //                                    });
    //                                }
    //
    //                            }
    //                        });
    //            }
    //        }).start();
    //    }
}
